package com.nulldev.util.web.nTubeExtensions.services.radio;

import java.util.Map;

import com.nulldev.util.VariableAPI.StringsUtil;
import com.nulldev.util.data.Arrays.maps.FastMaps;
import com.nulldev.util.web.HttpClient.HttpClient;

public class UniRadioAPI {

	public static abstract class Radio {
		private final String radio_name;

		private boolean supportsExtendedRadioInfo;
		private String radioIcon;

		private final boolean supportsPlaybackInfo;

		public Radio(final String radio_name) {
			this.supportsPlaybackInfo = false;
			this.radio_name = radio_name;
		}

		public Radio(final String radio_name, final boolean supportsPlaybackInfo) {
			this.supportsPlaybackInfo = supportsPlaybackInfo;
			this.radio_name = radio_name;
		}

		public boolean supportsPlaybackInfo() {
			return this.supportsPlaybackInfo;
		}

		public boolean supportsExtendedRadioInfo() {
			return this.supportsExtendedRadioInfo;
		}

		/* Radio->Core */

		public String getRadioName() {
			return this.radio_name;
		}

		public abstract String getStreamURL();

		/* Radio->ExtendedInfo */

		protected Radio setRadioIconURL(final String url) {
			this.radioIcon = url;
			return this;
		}

		public String getRadioIconURL() {
			return this.radioIcon;
		}

		/* Radio->PlaybackInfo */

		public abstract String getCurrentTrack();
	}

	/*
	 * Want to contribute to the UniRadioAPI? Go nuts.
	 * 
	 * ID Format: [COUNTRY].[SHORT_LOW_CASE]
	 */

	private static class Poland {
		private static class RNS extends Radio {
			public RNS() {
				/* [heavy laugh track 10 hours.ogg] */
				super("Radio Nowy Świat", true);
				this.setRadioIconURL("https://nowyswiat.online/wp-content/uploads/2019/03/logo-full-transparent.svg");
			}

			@Override
			public String getStreamURL() {
				try {
					return StringsUtil.substringBetweenO(HttpClient.getString().url("https://nowyswiat.online/").create().dataThenClose(), "' data-streamurl='",
							"'");
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}

			@Override
			public String getCurrentTrack() {
				try {
					return HttpClient.getString().url("https://nowyswiat.online/wp-content/plugins/wp-lunaradio/current.txt").create().dataThenClose();
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		}
	}

	private static final Map<String, Radio> RADIO_STATIONS = FastMaps.map();
	static {
		if (RADIO_STATIONS.isEmpty()) {
			/* Poland */
			RADIO_STATIONS.put("poland.rns", new Poland.RNS());
		}
	}
}
